<?php
namespace backend\models;

use Yii;
use yii\base\Model;
use yii\base\Exception;
use common\models\Employees;

class EmployeesForm extends Model
{
	public $realname;
	public $sex;
	public $id_card;
	public $mobile;
	public $address;
	public $status;
	public $entry;
	public $departure;
	public $head_img;
	public $id;

	private $_emp;

	public function formName()
    {
        return '';
    }

	public function rules()
	{
		return [
			[['realname', 'id_card', 'sex', 'mobile', 'address', 'entry'], 'required'],
			[['head_img', 'departure', 'id'], 'safe'],
		];
	}

    public function __construct($id = null, $config = []) {
        if (!empty($id)) {
            $emp = Employees::findOne($id);
            $this->realname = $emp->realname;
            $this->sex = $emp->sex;
            $this->id_card = $emp->id_card;
            $this->mobile = $emp->mobile;
            $this->address = $emp->address;
            $this->status = $emp->status;
            $this->entry = $emp->entry;
            $this->id = $emp->id;
        } else {
            $emp = new Employees();
        }
        $this->_emp = $emp;

        return parent::__construct($config);
    }

	public function addOrUpdate()
	{
		$emp = $this->_emp;
		$emp->realname = $this->realname;
		$emp->sex = $this->sex;
		$emp->id_card = $this->id_card;
		$emp->mobile = $this->mobile;
		$emp->address = $this->address;
		$emp->status = Employees::ACTIVE_STATUS;
		$emp->entry = strtotime($this->entry);

		if ($emp->save(false)) {

			return true;
		} else {

			return false;	
		}
	}


}	